package com.luboyan.CBillbill.advice;

import com.luboyan.CBillbill.exception.ConditionException;
import com.luboyan.CBillbill.dao.user.UserMoments;
import com.luboyan.CBillbill.dao.user.auth.UserRole;
import com.luboyan.CBillbill.service.user.auth.UserRoleService;
import com.luboyan.CBillbill.util.constat.UserConstant;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Aspect
@Component
@Order(1)
public class DataLimitedAdvice {

    @Autowired
    private TokenVerify tokenVerify;

    @Autowired
    private UserRoleService userRoleService;

    @Pointcut("@annotation(com.luboyan.CBillbill.annotation.DataLimited)")
    public void dataLimitedCheck() {
    }

    @Before("dataLimitedCheck()")
    public void RoleDataLimited(JoinPoint joinPoint) {
        Long userId = tokenVerify.getCurrentUserId();
        List<UserRole> userRoles = userRoleService.getUserRoleByUserId(userId);
        Set<String> userRolesSet = userRoles.stream().map(UserRole::getRoleCode).collect(Collectors.toSet());
        Object[] args = joinPoint.getArgs();
        //自定义各种逻辑即可
        for (Object arg : args) {
            if(arg instanceof UserMoments){
                String type = ((UserMoments) arg).getType();
                if(userRolesSet.contains(UserConstant.ROLE_CODE_LV1) && type.equals("0")){
                    throw new ConditionException("视频上传失败");
                }
            }
        }
    }
}
